package com.kc.sjwh.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.po.User;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.service.UserService;

/**
 * 用户管理
 * @author zhangguihua(hua7381@163.com)
 * date: 2017年8月4日
 */
@Controller
@RequestMapping("user")
public class UserAction extends BaseAction {
	static Logger logger = Logger.getLogger(UserAction.class);
	
	@Autowired
	UserService service;
	
	//打开列表页面
	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		return "user/userList";
	}

	//获取分页
	@ResponseBody
	@RequestMapping("getPage")
	public String getPage(HttpServletRequest req, int pageSize, int pageNum) {
		Page<User> page = service.getPage(pageSize, pageNum, super.getParamMap(req));
		return JSON.toJSONString(page);
	}

	//打开表单
	@RequestMapping("form")
	public String form(HttpServletRequest req, String act, String id) {
		req.setAttribute("act", act);
		req.setAttribute("id", id);
		req.setAttribute("actName", "add".equals(act)?"新增":"修改");
		return "user/userForm_"+act;
	}

	//表单加载
	@ResponseBody
	@RequestMapping("load4Update")
	public String load4Update(HttpServletRequest req, String id) throws Exception {
		Map m = service.load4Update(id);
		return JSON.toJSONString(m);
	}

	//新增
	@ResponseBody
	@RequestMapping("add")
	public String add(HttpServletRequest req, User one) throws Exception {
		if(StringUtils.isNotEmpty(one.getMm())) {//密码转MD5加密
			one.setMm(new Md5Hash(one.getMm()).toString());
		}
		one.setCjrxm(getLoginUser().getMc());
		service.add(one);
		return JSON.toJSONString(one.getId());
	}

	//修改
	@ResponseBody
	@RequestMapping("update")
	public String update(HttpServletRequest req, User one, String jsids) throws Exception {
		if(StringUtils.isNotEmpty(one.getMm())) {//密码转MD5加密
			one.setMm(new Md5Hash(one.getMm()).toString());
		}
		service.update(one, jsids);
		return JSON.toJSONString(null);
	}

	//删除
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest req, String id) {
		service.delete(id);
		return JSON.toJSONString(null);
	}
		
		
	/*
	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		return "user/userList";
	}
	
	@ResponseBody
	@RequestMapping("getPage")
	public String getPage(HttpServletRequest req, int pageSize, int pageNum) {
		Page<User> page = service.getPage(pageSize, pageNum, super.getParamMap(req));
		
		//数据转表格table
		Table table = new Table();
		table.add(new Tr("创建时间", "名称", "账号", "所属单位", "代理单位", "操作"));//表头
		for(User one : page.getList()) {//数据行
			table.add(new Tr(
				new Td(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(one.getCjsj())),
				new Td(one.getMc()),
				new Td(one.getZh()),
				new Td(one.getSsdwmc()),
				new Td(one.getDldwmc()),
				new Td("<a  data-toggle='modal' data-iframe='form?id="+one.getId()+"&pageType=edit' data-size='lg'>修改</a> &nbsp; " + 
						//"<a href='form?id="+one.getId()+"'>修改</a> &nbsp; "+ 
						"<a onclick=\"deletes('"+one.getId()+"')\">删除</a>")
			));
		}
		
		//返回结果
		Map data = new HashMap();
		data.put("total", page.getTotal());
		data.put("html", table.toString());
		return JSON.toJSONString(new AjaxRes(data));
	}
	
	@RequestMapping("form")
	public String form(HttpServletRequest req, String id, String pageType) {
		req.setAttribute("act", id==null?"新增":"修改");
		req.setAttribute("pageType", pageType);
		service.form(req, id);
		return "user/userForm";
	}
	
	@ResponseBody
	@RequestMapping("save")
	public String save(HttpServletRequest req, User one, String jsids) throws Exception {
		
		if(StringUtils.isNotEmpty(one.getMm())) {//密码转MD5加密
			one.setMm(new Md5Hash(one.getMm()).toString());
		}
		
		if(StringUtils.isEmpty(one.getId())) {//新增
			one.setId(UUID.randomUUID().toString().replace("-", ""));
			one.setCjrxm(getLoginUser().getMc());
			one.setZh(StringUtils.trim(one.getZh()));
			service.add(one, jsids);
			return JSON.toJSONString(new AjaxRes(one.getId()));
		} else {//修改
			if(StringUtils.isEmpty(one.getMm())) {
				one.setMm(null);
			}
			service.update(one, jsids);
			return JSON.toJSONString(new AjaxRes());
		}
	}
	
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest req, String id) {
		service.delete(id);
		return JSON.toJSONString(new AjaxRes());
	}
	*/
}
